<template>
    <div>
      <m-head title="我的收货地址"></m-head>
      <div class="list">
        <div class="item" v-for="item in list" :key="item.id">
          <p>{{item.name | firstName}}</p>
          <div class="info">
            <p class="name">
              {{item.name}} <span>{{item.phone}}</span>
            </p>
            <div class="addr">
              {{item.address}}
            </div>
          </div>
          <button @click="$router.push({path: './editaddress', query: {id: item.id}})">编辑</button>
        </div>
        <div class="addrnull" v-if="isEmpty">
          <p>收货地址为空，赶紧添加新地址吧！</p>
        </div>
        <div class="add">
          <button @click="$router.push('./editaddress')">添加新地址</button>
        </div>
      </div>
    </div>
</template>

<script>
import MHead from './MHeader'
import * as info from '../api/info'
export default {
  name: 'myAddress',
  data: function () {
    return {
      isEmpty: false,
      list: [
        /*{
          id: 1,
          default: true,
          name: '刘青山',
          phone: '132456345546',
          address: '江苏省南京市秦淮区石门坎104号首屏大厦4楼411室'
        },
        {
          id: 2,
          default: false,
          name: '生丽莎',
          tel: '132456345546',
          addr: '江苏省南京市秦淮区石门坎104号厚学网首屏大厦4楼411室'
        },
        {
          id: 3,
          default: false,
          name: '张盼盼',
          tel: '132456345546',
          addr: '江苏省南京市秦淮区石门坎104号厚学网首屏大厦4楼411室'
        }*/
      ]
    }
  },
  filters: {
    firstName: function (value) {
      return value.charAt(0)
    }
  },
  components: {
    'm-head': MHead
  },
  mounted () {
    this.getAddress()
  },
  methods: {
    getAddress () {
      info.getAddressList()
        .then(res => {
          console.log(res)
          if (!res.data) {
            this.isEmpty = true
          }
          this.list = res.data
        })
    }
  }
}
</script>
<style scoped>
.item{
  padding: .3rem;
  border-bottom: 1px solid #eee;
  display: flex;
  align-items: center;
}
.item>p{
  width: .6rem;
  height: .6rem;
  line-height: .6rem;
  -webkit-border-radius: 100%;
  -moz-border-radius: 100%;
  border-radius: 100%;
  text-align: center;
  flex-shrink: 0;
  background: #ed702d;
  color: #fff;
  margin-right: .2rem;
}
.info{
  flex-grow: 1;
}
.info .name{
  font-size: .4rem;
  color: #333;
}
.info .name span{
  font-size: .36rem;
  color: #888;
  margin-left: .2rem;
}
.info .addr{
  font-size: .38rem;
}
.info .addr span{
  background: #ed702d;
  color: #fff;
  padding: 0 2px;
  font-size: .32rem;
  -webkit-border-radius: 4px;
  -moz-border-radius: 4px;
  border-radius: 4px;
  margin-right: .16rem;
}
  .item button{
    flex-shrink: 0;
    margin-left: .2rem;
    border: none;
    background: #fff;
    border-left: 2px solid #eee;
    padding-left: .2rem;
    color: #888;
  }
  .add{
    margin: 1.2rem auto;
    text-align: center;
  }
  .add button{
    width: 4.6rem;
    height: 1rem;
    line-height: 1rem;
    border: none;
    background: #ed702d;
    color: #fff;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
  }
  .addrnull{
    text-align: center;
    font-size: .38rem;
    padding: 1rem 0 0;
    color: #666;
  }
</style>
